package com.bmadmin.service.impl;

import com.bmadmin.domain.*;
import com.bmadmin.mapper.SysPermissionsPOJODao;
import com.bmadmin.mapper.SysRolesPOJODao;
import com.bmadmin.mapper.SysRolesPermissionsPOJODao;
import com.bmadmin.service.SysRolesService;
import com.bmadmin.util.BMUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by 金玮良 on 2017-12-07.
 */
@Service
@Transactional
public class SysRolesServiceImpl implements SysRolesService {
    @Autowired
    SysRolesPOJODao sysRolesPOJODao;
    @Autowired
    SysRolesPermissionsPOJODao sysRolesPermissionsPOJODao;
    @Autowired
    SysPermissionsPOJODao sysPermissionsPOJODao;


    @Override
    public LayUiDataTablePOJO getRolesList(SysRolesPOJO sysRolesPOJO) {
        LayUiDataTablePOJO result = new LayUiDataTablePOJO();
        List<SysRolesPOJO> sysRolesPOJOS = sysRolesPOJODao.find(sysRolesPOJO);
        result.setCode(0);
        result.setMsg("查询成功");
        result.setCount(sysRolesPOJOS.size());
        result.setData(sysRolesPOJOS);

        return result;
    }

    @Override
    public NormalResult insertRole(SysRolesPOJO sysRolesPOJO) {
        NormalResult result = new NormalResult();

        SysRolesPOJO sysRolesPOJOForQuery = new SysRolesPOJO();
        sysRolesPOJOForQuery.setR_name(sysRolesPOJO.getR_name());
        List<SysRolesPOJO> sysRolesPOJOS = sysRolesPOJODao.find(sysRolesPOJOForQuery);
        if(sysRolesPOJOS.size()!=0){
            result.setCode("9999");
            result.setMsg("角色名称不可重复!");
        }else{
            sysRolesPOJO.setR_id(BMUtil.getUUID());
            sysRolesPOJODao.insertSelective(sysRolesPOJO);

            if(sysRolesPOJO.getPluginZtreeDataPOJOList().size()>0){
               List<PluginZtreeDataPOJO> sysPermissionsPOJOList= sysRolesPOJO.getPluginZtreeDataPOJOList();
               for(PluginZtreeDataPOJO sysPermissionsPOJOItem:sysPermissionsPOJOList){

                   SysRolesPermissionsPOJO sysRolesPermissionsPOJOForInertForFather = new SysRolesPermissionsPOJO();
                   sysRolesPermissionsPOJOForInertForFather.setR_id(sysRolesPOJO.getR_id());
                   sysRolesPermissionsPOJOForInertForFather.setP_id(sysPermissionsPOJOItem.getId());
                   sysRolesPermissionsPOJODao.insertSelective(sysRolesPermissionsPOJOForInertForFather);

                   List<PluginZtreeDataPOJO> sysPermissionsPOJOListForChildrens=sysPermissionsPOJOItem.getChildren();
                   if(sysPermissionsPOJOListForChildrens.size()>0){
                        for(PluginZtreeDataPOJO sysPermissionsPOJOChildren:sysPermissionsPOJOListForChildrens){
                            if(sysPermissionsPOJOChildren.isChecked()){
                                SysRolesPermissionsPOJO sysRolesPermissionsPOJOForInertForchildren = new SysRolesPermissionsPOJO();
                                sysRolesPermissionsPOJOForInertForchildren.setR_id(sysRolesPOJO.getR_id());
                                sysRolesPermissionsPOJOForInertForchildren.setP_id(sysPermissionsPOJOChildren.getId());
                                sysRolesPermissionsPOJODao.insertSelective(sysRolesPermissionsPOJOForInertForchildren);
                            }
                        }
                   }
               }
            }


            result.setCode("0000");
            result.setMsg("角色添加成功");
        }

        return result;
    }

    @Override
    public NormalResult deleteRole(SysRolesPOJO sysRolesPOJO) {
        NormalResult result = new NormalResult();

        int sqlReuslt = sysRolesPOJODao.deleteByR_id(sysRolesPOJO.getR_id());
        sysRolesPermissionsPOJODao.deleteByR_id(sysRolesPOJO.getR_id());

        if(sqlReuslt==1){
            result.setCode("0000");
            result.setMsg("角色删除成功");
        }else{
            result.setCode("9999");
            result.setMsg("角色删除异常");
        }

        return result;
    }

    @Override
    public NormalResult updateRole(SysRolesPOJO sysRolesPOJO) {
        NormalResult result = new NormalResult();

        SysRolesPOJO sysRolesPOJOForQuery = new SysRolesPOJO();
        sysRolesPOJOForQuery.setR_name(sysRolesPOJO.getR_name());
        List<SysRolesPOJO> sysRolesPOJOS = sysRolesPOJODao.find(sysRolesPOJOForQuery);

        if(sysRolesPOJOS.size()!=0&&(!sysRolesPOJOS.get(0).getR_id().equals(sysRolesPOJO.getR_id()))){
            result.setCode("9999");
            result.setMsg("角色名称不可重复!");
        }else{


            sysRolesPermissionsPOJODao.deleteByR_id(sysRolesPOJO.getR_id());

            if(sysRolesPOJO.getPluginZtreeDataPOJOList().size()>0){
                List<PluginZtreeDataPOJO> sysPermissionsPOJOList= sysRolesPOJO.getPluginZtreeDataPOJOList();
                for(PluginZtreeDataPOJO sysPermissionsPOJOItem:sysPermissionsPOJOList){

                    SysRolesPermissionsPOJO sysRolesPermissionsPOJOForInertForFather = new SysRolesPermissionsPOJO();
                    sysRolesPermissionsPOJOForInertForFather.setR_id(sysRolesPOJO.getR_id());
                    sysRolesPermissionsPOJOForInertForFather.setP_id(sysPermissionsPOJOItem.getId());
                    sysRolesPermissionsPOJODao.insertSelective(sysRolesPermissionsPOJOForInertForFather);

                    List<PluginZtreeDataPOJO> sysPermissionsPOJOListForChildrens=sysPermissionsPOJOItem.getChildren();
                    if(sysPermissionsPOJOListForChildrens.size()>0){
                        for(PluginZtreeDataPOJO sysPermissionsPOJOChildren:sysPermissionsPOJOListForChildrens){
                            if(sysPermissionsPOJOChildren.isChecked()){
                                SysRolesPermissionsPOJO sysRolesPermissionsPOJOForInertForchildren = new SysRolesPermissionsPOJO();
                                sysRolesPermissionsPOJOForInertForchildren.setR_id(sysRolesPOJO.getR_id());
                                sysRolesPermissionsPOJOForInertForchildren.setP_id(sysPermissionsPOJOChildren.getId());
                                sysRolesPermissionsPOJODao.insertSelective(sysRolesPermissionsPOJOForInertForchildren);
                            }
                        }
                    }
                }
            }

//            sysRolesPOJO.setR_id(BMUtil.getUUID());
            sysRolesPOJODao.update(sysRolesPOJO);
            result.setCode("0000");
            result.setMsg("角色修改成功");
        }

        return result;
    }

    @Override
    public NormalResult findRoleByIdIncludePermissionTree(SysRolesPOJO sysRolesPOJO) {
        NormalResult result = new NormalResult();

        List<SysPermissionsPOJO> sysRolesPermissionsPOJOS = sysPermissionsPOJODao.find(new SysPermissionsPOJO());

        SysRolesPermissionsPOJO sysRolesPermissionsPOJOForQuery = new SysRolesPermissionsPOJO();
        sysRolesPermissionsPOJOForQuery.setR_id(sysRolesPOJO.getR_id());
        List<SysRolesPermissionsPOJO> sysRolesPermissionsPOJOSResult = sysRolesPermissionsPOJODao.find(sysRolesPermissionsPOJOForQuery);

        List<PluginZtreeDataPOJO> rolesPermissionForZtree = new ArrayList<PluginZtreeDataPOJO>();
        for(SysPermissionsPOJO permissionsPOJOItem:sysRolesPermissionsPOJOS){
            PluginZtreeDataPOJO forAddRolesPermission = new PluginZtreeDataPOJO();

            forAddRolesPermission.setId(permissionsPOJOItem.getP_id());
            forAddRolesPermission.setPart_id(permissionsPOJOItem.getP_pid());
            forAddRolesPermission.setName(permissionsPOJOItem.getP_name());
            forAddRolesPermission.setOpen(true);

            boolean isChecked = false;
            for(SysRolesPermissionsPOJO sysRolesPermissionsPOJO:sysRolesPermissionsPOJOSResult){
                if(sysRolesPermissionsPOJO.getP_id().equals(permissionsPOJOItem.getP_id())){
                    isChecked = true;
                }
            }
            forAddRolesPermission.setChecked(isChecked);

            rolesPermissionForZtree.add(forAddRolesPermission);
        }

        for(PluginZtreeDataPOJO pluginZtreeDataPOJOItemOne:rolesPermissionForZtree){
            List<PluginZtreeDataPOJO> forAddChildren = new ArrayList<PluginZtreeDataPOJO>();

            for(PluginZtreeDataPOJO pluginZtreeDataPOJOItemTwo:rolesPermissionForZtree){
                if(pluginZtreeDataPOJOItemOne.getId().equals(pluginZtreeDataPOJOItemTwo.getPart_id())){
                    forAddChildren.add(pluginZtreeDataPOJOItemTwo);
                }
            }
            pluginZtreeDataPOJOItemOne.setChildren(forAddChildren);
        }

        List<PluginZtreeDataPOJO> resultForLevelOne = new ArrayList<PluginZtreeDataPOJO>();

        for(PluginZtreeDataPOJO pluginZtreeDataPOJOItem:rolesPermissionForZtree){
            if("0".equals(pluginZtreeDataPOJOItem.getPart_id())){
                resultForLevelOne.add(pluginZtreeDataPOJOItem);
            }
        }

        result.setCode("0000");
        result.setMsg("查询成功");
        result.setObject(resultForLevelOne);

        return result;
    }

    @Override
    public NormalResult findRoleByIdIncludePermissionForAside(SysRolesPOJO sysRolesPOJO) {
        NormalResult result = new NormalResult();

        SysRolesPermissionsPOJO sysRolesPermissionsPOJO = new SysRolesPermissionsPOJO();
        sysRolesPermissionsPOJO.setR_id(sysRolesPOJO.getR_id());
        List<SysRolesPermissionsPOJO> sysRolesPermissionsPOJOS = sysRolesPermissionsPOJODao.find(sysRolesPermissionsPOJO);

        List<SysPermissionsPOJO> rolesHasPermission = new ArrayList<SysPermissionsPOJO>();

        for(SysRolesPermissionsPOJO permissionItem:sysRolesPermissionsPOJOS){
            SysPermissionsPOJO sysPermissionsPOJOForQuery = new SysPermissionsPOJO();
            sysPermissionsPOJOForQuery.setP_id(permissionItem.getP_id());

            rolesHasPermission.add(sysPermissionsPOJODao.find(sysPermissionsPOJOForQuery).get(0));
        }

        for(SysPermissionsPOJO permissionItemFather:rolesHasPermission){
            List<SysPermissionsPOJO> sysPermissionsPOJOS = new ArrayList<SysPermissionsPOJO>();

            for(SysPermissionsPOJO permissionItemSun:rolesHasPermission){
                if(permissionItemFather.getP_id().equals(permissionItemSun.getP_pid())){
                    sysPermissionsPOJOS.add(permissionItemSun);
                }

            }

            permissionItemFather.setChildren(sysPermissionsPOJOS);

        }

        List<SysPermissionsPOJO> resultSysPermissionList = new ArrayList<SysPermissionsPOJO>();
        for(SysPermissionsPOJO permissionItem:rolesHasPermission){
            if("0".equals(permissionItem.getP_pid())){
                resultSysPermissionList.add(permissionItem);
            }
        }

        Collections.sort(resultSysPermissionList);

        result.setObject(resultSysPermissionList);
        return result;
    }

    @Override
    public NormalResult getFreePermissionTree() {
        NormalResult result = new NormalResult();

        List<SysPermissionsPOJO> sysRolesPermissionsPOJOS = sysPermissionsPOJODao.find(new SysPermissionsPOJO());

        List<PluginZtreeDataPOJO> rolesPermissionForZtree = new ArrayList<PluginZtreeDataPOJO>();
        for(SysPermissionsPOJO permissionsPOJOItem:sysRolesPermissionsPOJOS){
            PluginZtreeDataPOJO forAddRolesPermission = new PluginZtreeDataPOJO();

            forAddRolesPermission.setId(permissionsPOJOItem.getP_id());
            forAddRolesPermission.setPart_id(permissionsPOJOItem.getP_pid());
            forAddRolesPermission.setName(permissionsPOJOItem.getP_name());
            forAddRolesPermission.setOpen(false);

            rolesPermissionForZtree.add(forAddRolesPermission);
        }

        for(PluginZtreeDataPOJO pluginZtreeDataPOJOItemOne:rolesPermissionForZtree){
            List<PluginZtreeDataPOJO> forAddChildren = new ArrayList<PluginZtreeDataPOJO>();

            for(PluginZtreeDataPOJO pluginZtreeDataPOJOItemTwo:rolesPermissionForZtree){
                if(pluginZtreeDataPOJOItemOne.getId().equals(pluginZtreeDataPOJOItemTwo.getPart_id())){
                    forAddChildren.add(pluginZtreeDataPOJOItemTwo);
                }
            }
            pluginZtreeDataPOJOItemOne.setChildren(forAddChildren);
        }

        List<PluginZtreeDataPOJO> resultForLevelOne = new ArrayList<PluginZtreeDataPOJO>();

        for(PluginZtreeDataPOJO pluginZtreeDataPOJOItem:rolesPermissionForZtree){
            if("0".equals(pluginZtreeDataPOJOItem.getPart_id())){
                resultForLevelOne.add(pluginZtreeDataPOJOItem);
            }
        }

        result.setCode("0000");
        result.setMsg("查询成功");
        result.setObject(resultForLevelOne);

        return result;
    }
}
